<?php
/**
 * Created by PhpStorm.
 * User: 跳梁
 * Date: 2016/1/28
 * Time: 13:42
 */
class openhole{
    private static  $hole=array("申脉穴","照海穴","外关穴","足临泣穴","照海穴","公孙穴","后溪穴","内关穴","列缺穴");
    private static  $date=array("子时","丑时","寅时","卯时","辰时","巳时","午时","未时","申时","酉时","戌时","亥时");
    public function getIndex(){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $uid = Session::$user['uid'];
        $userName=Session::$user['userName'];
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $openhole = M("openhole");
        Session::switchCustomer($customerId);
        $t = date("H");
        $nowTime = date("Y-m-d");
        $dateIndex = self::getPeriodByHour($t);
        $result["nowTime"]=date("Y")."年".date("m")."月".date("d")."日";
        $result["date"]=self::$date[$dateIndex];
        $dateIndex++;
        $condition["date"]=array("eq",$nowTime);
        $openholeInfo = $openhole->where($condition)->field("s$dateIndex as hole")->find();
        if(!$openholeInfo){
            Output::instance()->resultCode(1,"没有查询到结果，请稍候再试。");
            return;
        }
        $holeIndex = explode(",",$openholeInfo["hole"]);
		$hole = array();
		foreach($holeIndex as $key=>$value){
			$value--;
			array_push($hole,self::$hole[$value]);
			$holeIndex[$key]["holeIndex"]=$value;
		}
		$result["hole"]=implode(",",$hole);
		$result["holeIndex"]=$holeIndex;
        $result["timeSlot"]=self::getHourByPeriod($result["date"]);
        Output::instance()->module("openhole","index")->object($result);
    }
    private static function getPeriodByHour($hour){
        $hour = (integer)$hour;
        if($hour<0 || $hour>23){
            return false;
        }
        $timediff=($hour-23+24)%24;//确保时间差为正数
        $offset=$timediff%2==1?floor($timediff/2):ceil($timediff/2);
        return $offset;
    }
    private static function getHourByPeriod($period){
        if(!in_array($period,self::$date)){
            return false;
        }
        $start = (23 + array_search($period,self::$date)*2)%24;
        $end   = ($start + 2)%24;
        return $start . '时至' . $end."时";
    }
}